package com.huweilong.tingshu.servlet.album;

import com.huweilong.tingshu.repository.AlbumRepo;
import com.huweilong.tingshu.util.Log;
import com.huweilong.tingshu.view_object.UserVO;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 是阿秋啊
 * @date 2022/07/22 11:36
 **/
@WebServlet("/studio/album/create.do")
public class CreateDoServlet extends HttpServlet {
    private final AlbumRepo albumRepo = new AlbumRepo();
    @Override
    @SneakyThrows
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        UserVO currentUser = null;
        HttpSession session = req.getSession(false);
        if (session != null) {
            currentUser = (UserVO) session.getAttribute("currentUser");
        }

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/plain");
        PrintWriter writer = resp.getWriter();
        if (currentUser == null) {
            Log.println("UploadDoServlet用户未登录");
            resp.sendRedirect("/studio/user/login.html");
            return;
        }

        String title = req.getParameter("title");
        String cover = req.getParameter("cover");
        int state = 1;
        albumRepo.insert(currentUser.uid,title,cover,state);

//        resp.setCharacterEncoding("utf-8");
//        resp.setContentType("text/plain");
//        resp.getWriter().println("创建成功");
        resp.sendRedirect("/studio/album/list.html");
    }
}